<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>数组声明式方法</title>
</head>
<body>
  <script>
    /* 
    自定义实现一组数组声明式方法
      1. map()
      2. reduce()
      3. filter()
      4. find()
      5. findIndex()
      6. every()
      7. some()
    */
    /* 
    需求:
    1. 产生一个每个元素都比原来大10的新数组
    2. 得到所有奇数的和
    3. 得到值大于8且下标是偶数位的元素组成的数组
    4. 找出一个值大于8且下标是偶数位的元素
    5. 找出一个值大于8且下标是偶数位的元素的下标
    6. 判断下标为偶数的元素是否都为奇数
    7. 判断是否有下标为偶数的元素值为奇数
    */

    const arr = [1, 3, 6, 9, 15, 19, 16]

    console.log(arr.map((item, index) => item + 10))
    console.log(arr.reduce((preTotal, item, index) => {
      return preTotal + (item%2===1 ? item : 0)
    }, 0))
    console.log(arr.filter((item, index) => item>8 && index%2===0))
    console.log(arr.find((item, index) => item>8 && index%2===0))
    console.log(arr.findIndex((item, index) => item>8 && index%2===0))
    console.log(arr.every((item, index) => index%2===1 || item%2===1))
    console.log(arr.some((item, index) => index%2===1 || item%2===1))
  </script>

  <script>
    /* 实现map() */
    Array.prototype.map = function (callback) {
      const arr = []
      for (let index = 0; index < this.length; index++) {
        arr.push(callback(this[index], index))
      }
      return arr
    }

    /* 实现reduce() */
    Array.prototype.reduce = function (callback, initValue) {
      let result = initValue
      for (let index = 0; index < this.length; index++) {
        // 调用回调函数将返回的结果赋值给result
        result = callback(result, this[index], index)
      }
      return result
    }

    /* 实现filter() */
    Array.prototype.filter = function (callback) {
      const arr = []
      for (let index = 0; index < this.length; index++) {
        if (callback(this[index], index)) {
          arr.push(this[index])
        }
      }
      return arr
    }

    /* 实现find() */
    Array.prototype.find = function (callback) {
      for (let index = 0; index < this.length; index++) {
        if (callback(this[index], index)) {
          return this[index]
        }
      }
      return undefined
    }

    /* 实现findIndex() */
    Array.prototype.findIndex = function (callback) {
      for (let index = 0; index < this.length; index++) {
        if (callback(this[index], index)) {
          return index
        }
      }
      return -1
    }

     /* 实现every() */
     Array.prototype.every = function (callback) {
      for (let index = 0; index < this.length; index++) {
        if (!callback(this[index], index)) { // 只有一个结果为false, 直接返回false
          return false
        }
      }
      return true
    }


    /* 实现some() */
    Array.prototype.some = function (callback) {
      for (let index = 0; index < this.length; index++) {
        if (callback(this[index], index)) { // 只有一个结果为true, 直接返回true
          return true
        }
      }
      return false
    }
  </script>
</body>
</html>